/**
 * @constructor
 * 
 * @param{array} sprites The array of Sprite objects that will be drawn during the gameloop
 * @param{Canvas} canvas The canvas JQuery object on which to draw.
 */
function GameLoop(sprites, canvas, fps) {
	
	this.sprites = sprites;
	this.canvas = canvas;
	
	this.fps = fps;
	this.updateTimeMs = 1000/fps;
	
	this.fpsMeasurementStartTimeMs;
	this.fpsCount = 0;
	
	this.actualFps = fps;
	
	/**
	 * @public
	 */
	this.start = function() {
		var self = this;
		
		d = new Date();
		this.fpsMeasurementStartTimeMs = d.getTime();
		
		setInterval(function() {
			self.loop();
		}, self.updateTimeMs);
		
	};
	
	/**
	 * @private
	 */
	this.loop = function()  {
		var self = this;
		
		self.canvas.clearCanvas();
				
		$.each(self.sprites,function(index) {
		    this.update();
		});
		
		self.canvas.drawText({
			  fillStyle: "#000",
			  x: 30, y: 10,
			  font: "9pt Verdana, sans-serif",
			  text: self.actualFps + " fps"
			});
		
		self.fpsCount += 1;
		d = new Date();
		currentTime = d.getTime();
		timeDiff = currentTime - self.fpsMeasurementStartTimeMs;
		if(timeDiff>1000) {
			self.actualFps = parseFloat(this.fpsCount/(timeDiff/1000)).toFixed(1);
			d = new Date();
			self.fpsMeasurementStartTimeMs = d.getTime();
			self.fpsCount = 0;
		}
	};
}